package com.woniu.auth.service.impl;

import com.woniu.auth.entity.User;
import com.woniu.auth.exception.AuthException;
import com.woniu.auth.mapper.UserMapper;
import com.woniu.auth.service.UserService;
import com.woniu.auth.vo.LoginVo;
import com.woniu.commons.util.JWTUtil;
import com.woniu.commons.util.TokenEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public User login(LoginVo vo) {
        User user = userMapper.findByUsername(vo.getUsername());
        System.out.println("user:" + user.getUsername());
        if (user == null) {
            throw new AuthException("用户不存在");
        } else if (!user.getPassword().equals(vo.getPassword())) {
            throw new AuthException("密码错误");
        }
        return user;
    }

    @Override
    public List<String> findPerms(String token) {
        if (JWTUtil.verify(token) == TokenEnum.TOKEN_BAD) {

        }
        //TODO 获取用户id
        int userId = JWTUtil.getUserId(token);

        //TODO 获取用户权限
        return userMapper.findPerms(userId);
    }
}
